package com.songlanyun.modules.collect.controller.api;

import com.songlanyun.common.annotation.RepeatSubmit;
import com.songlanyun.common.utils.PageUtils;
import com.songlanyun.common.utils.R;
import com.songlanyun.modules.account.controller.common.AbstractApiController;
import com.songlanyun.modules.collect.entity.UserCollectGoods;
import com.songlanyun.modules.collect.model.dto.UserCollectGoodsDTO;
import com.songlanyun.modules.collect.service.UserCollectGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * 用户收藏商品
 *
 * @author zx
 * @email
 * @date 2021-03-08 10:27:59
 */
@RestController
@RequestMapping("api/collect/goods")
public class UserCollectGoodsController extends AbstractApiController {
    @Autowired
    private UserCollectGoodsService userCollectGoodsService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        Long userId = getUserId(true);
        params.put("user_id", userId);
        PageUtils page = userCollectGoodsService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 收藏
     */
    @PutMapping("/save")
    public R save(@RequestBody UserCollectGoodsDTO dto) {
        Long userId = getUserId(true);
        UserCollectGoods goods = userCollectGoodsService.addEntity(userId, dto);
        return R.ok().put("data", goods);
    }

    /**
     * 取消收藏
     */
    @DeleteMapping("/delete/{goodsId}")
    public R delete(@PathVariable("goodsId") Long goodsId) {
        userCollectGoodsService.delCollect(getUserId(true), goodsId);
        return R.ok();
    }


    /**
     * 会员是否已收藏该商品
     */
    @GetMapping("/is-collect/{goodsId}")
    public R isCollect(@PathVariable("goodsId") Long goodsId) {
        Long userId = getUserId(false);
        Boolean b = false;
        if (userId != 0) {
            b = userCollectGoodsService.isCollect(userId, goodsId);
        }
        return R.ok().put("data", b);
    }

}
